/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tuplestoreserver;

import core.IServerCallBack;
import core.ProtocolSD;
import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import logging.LOG;
import protocol.TSFileTag;

/**
 *
 * @author Jorge
 */
public class MasterServerListener implements ProtocolSD.IProtocolListener{

    private boolean close =false;

    private ProtocolSD.Operation operation;

    private List<Integer> currentKeys= new LinkedList<Integer>();

    private int serverKey;

    private int ticket;


   private Server server;

    public MasterServerListener(Server server) {
        this.server=server;
    }

    public void onReadHeader(int operation, int numArgs, ProtocolSD protocolSD) {
        this.operation= ProtocolSD.Operation.values()[operation];
        LOG.append("Recebido pedido"+this.operation.name());
        currentKeys.clear();
        serverKey=0;
    }

    public void onReadArgument(byte[] data, ProtocolSD protocolSD) {
        switch(operation){
            case KEYSET : handlerReadArgumentKeySet(data,protocolSD);break;
            case NOTIFY : handlerReadArgumentNotify(data,protocolSD);break;
            case REMOVE : handlerReadArgumentRemove(data,protocolSD);break;
            case STATUS : break;
        }
    }

    public void onReadArgumentSize(int size, ProtocolSD protocolSD) {

    }

    public void onReadAllArguments(ProtocolSD protocolSD) {
        protocolSD.getKey().interestOps(SelectionKey.OP_WRITE);
        switch(operation){
            case KEYSET : handlerReadAllArgumentKeySet(protocolSD);break;
            case NOTIFY : handlerReadAllArgumentNotify(protocolSD);break;
            case REMOVE : handlerReadAllArgumentRemove(protocolSD);
                          
            break;
            case STATUS : break;
        }
    }

    public void onWriteComplete(ProtocolSD protocolSD) {
        switch(operation){
            case KEYSET : 
            case NOTIFY : 
            case REMOVE : 
            case STATUS : close=true;
        try {
            protocolSD.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        }
    }

    public void setCallBack(IServerCallBack callback) {

    }

    public IServerCallBack getCallBack() {
        return null;
    }

    public void onReadParcialParam(byte[] readedBytes, int offset, int limit, ProtocolSD protocolSD) {

    }

    public void onAcceptConnect() {

    }

    public boolean isClose() {
       return close;
    }

    private void handlerReadArgumentRemove(byte[] data, ProtocolSD protocolSD) {
        currentKeys.add(ProtocolSD.byteArrayToInt(data,0));
    }

    private void handlerReadAllArgumentRemove(ProtocolSD protocolSD) {
        int i = currentKeys.get(0);
        server.removeKey(i);
        protocolSD.write(new byte[]{(byte)ProtocolSD.Operation.REMOVE.ordinal(),1});
        protocolSD.writeInt(5);
        protocolSD.writeInt(i);
        protocolSD.write(ProtocolSD.SUCCESS);

    }

    private void handlerReadArgumentNotify(byte[] data, ProtocolSD protocolSD) {
        //server.cancelPendentDrain(ProtocolSD.byteArrayToInt(data, 0), ProtocolSD.byteArrayToInt(data, 4));
    }

    private void handlerReadAllArgumentNotify(ProtocolSD protocolSD) {
        try{
        protocolSD.close();
        }catch(IOException exception){
            exception.printStackTrace();
        }

    }

    private void handlerReadArgumentKeySet(byte[] data, ProtocolSD protocolSD) {
       
    }

    private void handlerReadAllArgumentKeySet(ProtocolSD protocolSD) {
        Map<Integer,TSFileTag> map =server.getKeyMapping();
        int num = server.getServerKey();
        protocolSD.write(new byte[]{(byte)ProtocolSD.Operation.KEYSET.ordinal(),(byte)map.size()});
        for(Integer integer : map.keySet()){
            protocolSD.writeInt(8);
            protocolSD.writeInt(integer);
            protocolSD.writeInt(num);
        }
    }

    
}
